///Deref <T> trait用于自定义解除引用运算符(*)的行为。
use :: std::ops::Deref;
use std::ops::DerefMut;

struct MyBox<T> {
    value: T,
}

impl <T> Deref for MyBox<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}


#[test]
pub fn deref_demo(){
    let  b = MyBox{value: 10};
    let a = *b;
    println!("{}", a);

}


//元组结构体
struct MyBox2<T>(T);

impl <T> MyBox2<T> {
    fn new(x: T) -> MyBox2<T> {
        MyBox2(x)
    }
}
//实现Deref
impl <T> Deref for MyBox2<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
//实现DerefMut
impl <T> DerefMut for MyBox2<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

fn print(m: i32){
    println!("{}", m);
}

#[test]
pub fn deref_demo2(){
    let mut b = MyBox2::new(243);
    print(*b); //取值

    *b = 34; //修改值
    print(*b);
}


